package com.topscomm.mpm.script;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectSaleEntity;
import com.topscomm.mpm.service.impl.MpmProjectSaleService;
import com.topscomm.mpm.utils.ConvertTimeUtil;
import com.topscomm.tap.common.ParameterCache;
import org.springframework.transaction.annotation.Transactional;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.mpm.MpmScript;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;

public class MpmProjectScript extends MpmScript {

    /**
     * @Title: projectApproval
     * @description:立项审批结束后回写项目状态和立项时间，同时将状态细化：任命状态
     * @param service
     * @param entity
     * @throws Exception
     * @author: lanjianwei
     * @date: 2021年1月19日下午3:32:07
     * @modify:
    */
    @Transactional
    public void projectUpdate(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil
				.getBean("mpmProjectService");
        MpmProjectSaleService mpmProjectSaleService = (MpmProjectSaleService) SpringUtil.getBean("mpmProjectSaleService");
		//查询项目entity
		MpmProjectEntity mpmProjectEntity=mpmProjectService.queryById(sourceid);
        MpmProjectSaleEntity mpmProjectSaleEntity = mpmProjectSaleService.queryById(sourceid);
		//更新字段
		mpmProjectEntity.setProjectstate("1");
        Date now = new Date();
        mpmProjectEntity.setFinishdate(now);
        String remindAfterProject = ParameterCache.getValue("remindAfterProject", "3");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DAY_OF_MONTH, ConvertUtil.convertToInt(remindAfterProject));
        mpmProjectEntity.setReminddate(calendar.getTime());

		//更新状态：任命状态（立项审批结束到任命审批结束）
        mpmProjectEntity.setSchedulestate(MpmSystemConst.MpmScheduleState.AppointState);
		mpmProjectService.updateEntity(mpmProjectEntity);
        // 项目来源选择为1个机会点时，立项审批通过后将项目名称反向同步到机会点中
        if (MpmSystemConst.MpmProjectSource.chance.equals(mpmProjectSaleEntity.getProjectsource()) && !mpmProjectSaleEntity.getChanceclue().contains(",")) {
            mpmProjectService.syncProjectNameToChance(mpmProjectEntity, mpmProjectSaleEntity);
        }
    }

    /**
     * @Title: projectDisApproval
     * @description: 决策组审批有人不同意时通知项目经理并开放项目经理取消审批的权限
     * @param service
     * @param entity
     * @throws Exception
     * @author: zhangjunyao
     * @date: 2021年11月19日
     * @modify:
     */
    @Transactional
    public void setDisApprovalAndSendMessage(ITapService service, TapEntity entity) throws Exception {
        CboApprovalNodeInsEntity nodeIns = (CboApprovalNodeInsEntity) entity;
        MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
        // 此为退回脚本，发送邮件通知项目经理
        mpmProjectService.sendMessageToManager(nodeIns);
    }
}
